home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / snpd1292.zip / HUGEREAD.C < prev    next >
C/C++ Source or Header  |  1992-12-26  |  4KB  |  148 lines

  1. /*
  2. **  HUGEREAD.C - "Universal" PC read and write functions using huge data
  3. **               and far pointers.
  4. **
  5. **  NOTES:
  6. **
  7. **  1. If these functions are called with a prototype in scope, passed
  8. **     parameters will be coerced to the proper data types.
  9. **
  10. **  2. Since these call read() and write(), all normal mode flags which
  11. **     are supported by individual compilers will be honored.
  12. **
  13. **  3. In small data memory models (S, T, and M), an intermediate buffer
  14. **     is allocated and used. In large data models (L and C), the data
  15. **     are read/written directly from/to target memory.
  16. **
  17. **  Original Copyright 1992 by Bob Stout as part of
  18. **  the MicroFirm Function Library (MFL)
  19. **
  20. **  This subset version is hereby donated to the public domain.
  21. */
  22.  
  23. #include <dos.h>
  24. #include <io.h>
  25. #include <stdlib.h>
  26. #include <stddef.h>
  27.  
  28. #ifndef min
  29.  #define min(x,y) (((x) <= (y)) ? (x) : (y))
  30. #endif
  31.  
  32. #ifndef MK_FP
  33.  #define MK_FP(seg,offset) \
  34.         ((void _far *)(((unsigned long)(seg)<<16) | (unsigned)(offset)))
  35. #endif
  36.  
  37. /*
  38. **  Get the largest buffer possible.
  39. */
  40.  
  41. static size_t gettmp(char **buf)
  42. {
  43.       size_t bufsiz;
  44.  
  45.       for (bufsiz = 0x4000; bufsiz >= 128; bufsiz >>= 1)
  46.       {
  47.             if (NULL != (*buf = (char *) malloc(bufsiz)))
  48.                   return bufsiz;
  49.       }
  50.       return 0;
  51. }
  52.  
  53. /*
  54. **  Normalize a far pointer
  55. */
  56.  
  57. void _far *farnormal(void _far *ptr)
  58. {
  59.       size_t seg, ofs;
  60.  
  61.       seg = FP_SEG(ptr);
  62.       ofs = FP_OFF(ptr);
  63.       return MK_FP(seg + (ofs >> 4), ofs & 0xf);
  64. }
  65.  
  66. /*
  67. **  Read any size block to anywhere in memory
  68. */
  69.  
  70. long hugeread(int fh, char _far *buf, long size)
  71. {
  72.       long count;
  73.       size_t bufsiz;
  74.       char *tmp;
  75.       long ercode = size;
  76.  
  77.       if (4 > sizeof(void *))
  78.       {
  79.             if (0 == (bufsiz = gettmp(&tmp)))
  80.                   return -1L;
  81.       }
  82.       else
  83.       {
  84.             tmp = (char *)buf;
  85.             bufsiz = 0x4000;
  86.       }
  87.  
  88.       while (0 < (count = min(size, (long)bufsiz)))
  89.       {
  90.             int i, numread = read(fh, tmp, (size_t)count);
  91.  
  92.             if (1 > numread || numread != (int)count)
  93.                   return -1L;
  94.             if (4 > sizeof(void *))
  95.             {
  96.                   for (i = 0; i < count; ++i)
  97.                         buf[i] = tmp[i];
  98.             }
  99.             buf = farnormal(buf + count);
  100.             size -= count;
  101.             if (2 < sizeof(void *))
  102.                   tmp = (char *)buf;
  103.       }
  104.       return ercode;
  105. }
  106.  
  107. /*
  108. **  Write any size block from anywhere in memory
  109. */
  110.  
  111. long hugewrite(int fh, char _far *buf, long size)
  112. {
  113.       long count;
  114.       size_t bufsiz;
  115.       char *tmp;
  116.       long ercode = size;
  117.  
  118.       if (4 > sizeof(void *))
  119.       {
  120.             if (0 == (bufsiz = gettmp(&tmp)))
  121.                   return -1L;
  122.       }
  123.       else
  124.       {
  125.             tmp = (char *)buf;
  126.             bufsiz = 0x4000;
  127.       }
  128.  
  129.       while (0 < (count = min(size, (long)bufsiz)))
  130.       {
  131.             int i, numwrite;
  132.  
  133.             if (4 > sizeof(void *))
  134.             {
  135.                   for (i = 0; i < count; ++i)
  136.                         tmp[i] = buf[i];
  137.             }
  138.             numwrite = write(fh, tmp, (size_t)count);
  139.             if (1 > numwrite || numwrite != (int)count)
  140.                   return -1L;
  141.             buf = farnormal(buf + count);
  142.             size -= count;
  143.             if (2 < sizeof(void *))
  144.                   tmp = (char *)buf;
  145.       }
  146.       return ercode;
  147. }
  148.